<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>简历管理中心 - 校园智能生活助手</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.7.2/font/bootstrap-icons.css" rel="stylesheet">
    <style>
        .job-card {
            transition: transform 0.2s;
        }
        .job-card:hover {
            transform: translateY(-2px);
        }
        .application-item {
            border-left: 4px solid #007bff;
            background-color: #f8f9fa;
        }
    </style>
</head>
<body>
    <nav class="navbar navbar-expand-lg navbar-dark bg-primary">
        <div class="container">
            <a class="navbar-brand" href="{{ url_for('dashboard') }}">
                <i class="bi bi-house-door"></i> 校园智能生活助手
            </a>
            <div class="navbar-nav ms-auto">
                <a class="nav-link" href="{{ url_for('admin') }}">管理后台</a>
                <a class="nav-link" href="{{ url_for('logout') }}">退出登录</a>
            </div>
        </div>
    </nav>

    <div class="container mt-4">
        <div class="row">
            <div class="col-12">
                <h2><i class="bi bi-file-earmark-text"></i> 简历管理中心</h2>
                <p class="text-muted">查看和管理所有学生投递的简历</p>
                <hr>
            </div>
        </div>

        <!-- 简历统计 -->
        <div class="row mb-4">
            <div class="col-md-3">
                <div class="card text-center">
                    <div class="card-body">
                        <h5 class="card-title text-primary">总简历数</h5>
                        <h3 id="totalResumes" class="text-primary">0</h3>
                    </div>
                </div>
            </div>
            <div class="col-md-3">
                <div class="card text-center">
                    <div class="card-body">
                        <h5 class="card-title text-success">今日新增</h5>
                        <h3 id="todayResumes" class="text-success">0</h3>
                    </div>
                </div>
            </div>
            <div class="col-md-3">
                <div class="card text-center">
                    <div class="card-body">
                        <h5 class="card-title text-info">本周新增</h5>
                        <h3 id="weekResumes" class="text-info">0</h3>
                    </div>
                </div>
            </div>
            <div class="col-md-3">
                <div class="card text-center">
                    <div class="card-body">
                        <h5 class="card-title text-warning">活跃岗位</h5>
                        <h3 id="activeJobs" class="text-warning">0</h3>
                    </div>
                </div>
            </div>
        </div>

        <!-- 简历列表 -->
        <div class="row">
            <div class="col-12">
                <div class="card">
                    <div class="card-header d-flex justify-content-between align-items-center">
                        <h5><i class="bi bi-list-ul"></i> 所有简历</h5>
                        <button class="btn btn-outline-primary btn-sm" onclick="loadApplications()">
                            <i class="bi bi-arrow-clockwise"></i> 刷新
                        </button>
                    </div>
                    <div class="card-body">
                        <div id="applicationsList">
                            <!-- 简历列表将通过JavaScript动态加载 -->
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>



    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>
    <script>
        // 页面加载时获取简历列表
        document.addEventListener('DOMContentLoaded', function() {
            loadApplications();
        });

        // 加载所有简历
        function loadApplications() {
            fetch('/api/admin/all_applications')
            .then(response => {
                if (!response.ok) {
                    throw new Error('网络请求失败');
                }
                return response.json();
            })
            .then(data => {
                if (data.success) {
                    displayApplications(data.applications);
                    updateStatistics(data.applications);
                } else {
                    console.error('获取简历列表失败:', data.error || data.message || '未知错误');
                    alert('获取简历列表失败：' + (data.error || data.message || '未知错误'));
                }
            })
            .catch(error => {
                console.error('Error:', error);
                alert('网络错误，请稍后重试');
            });
        }

        // 显示简历列表
        function displayApplications(applications) {
            const applicationsList = document.getElementById('applicationsList');
            applicationsList.innerHTML = '';

            if (applications.length === 0) {
                applicationsList.innerHTML = '<p class="text-muted text-center">暂无简历投递</p>';
                return;
            }

            let html = '<div class="table-responsive">';
            html += `
                <table class="table table-hover">
                    <thead class="table-light">
                        <tr>
                            <th>学生信息</th>
                            <th>应聘岗位</th>
                            <th>简历文件</th>
                            <th>投递时间</th>
                            <th>操作</th>
                        </tr>
                    </thead>
                    <tbody>
            `;

            applications.forEach(app => {
                html += `
                    <tr>
                        <td>
                            <div>
                                <strong>${app.student_name}</strong><br>
                                <small class="text-muted">学号：${app.student_id}</small><br>
                                <small class="text-muted">${app.major} - ${app.class_name}</small>
                            </div>
                        </td>
                        <td>
                            <div>
                                <strong>${app.job_title}</strong><br>
                                <small class="text-muted">
                                    <i class="bi bi-geo-alt"></i> ${app.work_location} |
                                    <i class="bi bi-currency-yen"></i> ${app.hourly_wage}元/小时
                                </small>
                            </div>
                        </td>
                        <td>
                            <span class="badge bg-primary">
                                <i class="bi bi-file-earmark-word"></i> ${app.resume_filename || '简历文件'}
                            </span>
                        </td>
                        <td>
                            <small>${new Date(app.application_time).toLocaleString()}</small>
                        </td>
                        <td>
                            <a href="/api/admin/preview_resume/${app.id}" class="btn btn-sm btn-outline-info me-1" target="_blank">
                                <i class="bi bi-eye"></i> 预览
                            </a>
                            <a href="/api/admin/download_resume/${app.id}" class="btn btn-sm btn-outline-primary" target="_blank">
                                <i class="bi bi-download"></i> 下载
                            </a>
                        </td>
                    </tr>
                `;
            });

            html += '</tbody></table></div>';
            applicationsList.innerHTML = html;
        }

        // 更新统计信息
        function updateStatistics(applications) {
            const total = applications.length;
            const today = new Date();
            const todayStart = new Date(today.getFullYear(), today.getMonth(), today.getDate());
            const weekStart = new Date(today.getTime() - 7 * 24 * 60 * 60 * 1000);

            const todayCount = applications.filter(app => 
                new Date(app.application_time) >= todayStart
            ).length;

            const weekCount = applications.filter(app => 
                new Date(app.application_time) >= weekStart
            ).length;

            const uniqueJobs = new Set(applications.map(app => app.job_title)).size;

            document.getElementById('totalResumes').textContent = total;
            document.getElementById('todayResumes').textContent = todayCount;
            document.getElementById('weekResumes').textContent = weekCount;
            document.getElementById('activeJobs').textContent = uniqueJobs;
        }


    </script>
</body>
</html>